var savestrp = [];
var sDateFtpDownload;
var eDateFtpDownload;
var i;
var FindCfmFtpDownload;
var SearchFtpDownloadId;
var SearchFtpDownloadArea;
var SearchFtpDownloadDefine;
var savestr;
var childGridKey;
var flag;

$(document).ready(function() {
	var session = sessionStorage.getItem("session");
	if (!session) {
		window.parent.location = "../../login.html";
	}
	$('#sDateFtpDownload').datetimebox('setValue', ThreeDayBefore);
	
	$("input", $("#DeviceIdFtpDownload").next("span")).click(function() {
		$('#QueryFtpDownloadTab').window('open');
	});
	
	SearchFtpDownloadId = function() {
		var aDeviceid;
		savestrp = [""];
		flag = 1;
		aDeviceid = $("#QueryFtpDownloadId").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (i = 0; i < aDeviceid.length; i++) {
			savestrp[i] = $("#QueryFtpDownloadId").getCell(aDeviceid[i], 'deviceid');
		}
		console.log(savestrp);
		$("#DeviceIdFtpDownload").textbox('setValue', savestrp);
		$('#QueryFtpDownloadTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	SearchFtpDownloadArea = function() {
		var aArea;
		savestrp = [""];
		flag = 2;
		aArea = $("#QueryFtpDownloadArea").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (i = 0; i < aArea.length; i++) {
			savestrp[i] = $("#QueryFtpDownloadArea").getCell(aArea[i], 'text');
		}
		console.log(savestrp);
		$("#DeviceIdFtpDownload").textbox('setValue', savestrp);
		$('#QueryFtpDownloadTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}
	SearchFtpDownloadDefine = function() {
		var aDefine;
		savestrp = [""];
		flag = 3;
		aDefine = $("#QueryFtpDownloadDefine").jqGrid('getGridParam', 'selarrrow'); //获取选中行号		
		for (i = 0; i < aDefine.length; i++) {
			savestrp[i] = $("#QueryFtpDownloadDefine").getCell(aDefine[i], 'probeGroup');
		}
		console.log(savestrp);
		$("#DeviceIdFtpDownload").textbox('setValue', savestrp);
		$('#QueryFtpDownloadTab').window('close');
		//		count: $("#jqGrid").getCell(rowKey, "count"),
	}

	$("#QueryFtpDownloadGrid").jqGrid({
		colModel: [{
			label: '任务编号',
			name: 'taskid'
		}, {
			label: '设备编码',
			name: 'deviceid'
		}, {
			label: '序列号',
			name: 'seqnum'
		}, {
			label: '类型',
			name: 'type'
		}, {
			label: '执行时间',
			name: 'executeDate'
		}, {
			label: '执行耗时',
			name: 'executeTimespent'
		}, {
			label: '服务器地址',
			name: 'serverAddress'
		}, {
			label: '端口',
			name: 'port'
		}, {
			label: '服务器IP',
			name: 'serverIP'
		}],
		pagerpos: 'center',
		viewrecords: true,
		height: 480,
		rowNum: 17,
		autowidth: true,
		loadonce: true,
		page: 1,
		subGrid: true, // set the subGrid property to true to show expand buttons for each row
		subGridRowExpanded: showChildGrid, // javascript function that will take care of showing the child grid
		loadtext: '正在加载数据',
		pager: "#QueryFtpDownloadGridPager",
		sortorder: "asc",
		sortname: 'id'
	});

	$(window).bind('resize', function() {
		$("#QueryFtpDownloadGrid").setGridWidth($(window).width() * 1);
		$("#QueryFtpDownloadGrid").setGridHeight($(window).height()-110);
	});

	ExportFile = function() {
		$('#QueryFtpDownloadGrid').jqGrid('setGridParam', {
			rowNum: 1000,
		}).trigger('reloadGrid');
		var title = "Ftp下载测试报告";
		getXlsFromTbl('QueryFtpDownloadGrid', 'gridtable', title, true);
		setTimeout(BackGrid(), 500);
		function BackGrid() {
			$('#QueryFtpDownloadGrid').jqGrid('setGridParam', {
				rowNum: 17,
			}).trigger('reloadGrid');
		}	
	}
	$('#QueryFtpDownloadGrid').navGrid("#QueryFtpDownloadGridPager", {
		search: false, // show search button on the toolbar
		add: false,
		edit: false,
		del: false,
		refresh: true
	});

	FindCfmFtpDownload = function() {
		sDateFtpDownload = $('#sDateFtpDownload').datetimebox('getValue').substr(0, 10);
		eDateFtpDownload = $('#eDateFtpDownload').datetimebox('getValue').substr(0, 10);
		if (DateRight(sDateFtpDownload, eDateFtpDownload)) {
			savestr = savestrp;
			if ((sDateFtpDownload != "") && (eDateFtpDownload != "") && (savestr != "")) {
				var myurl = IP + "QueryFtpDownData?callback=?&session=" + session + "&flag=" + flag + "&savestr=" + savestr + "&sDate=" + sDateFtpDownload + "&eDate=" + eDateFtpDownload;
				$('#QueryFtpDownloadGrid').jqGrid('setGridParam', {
					url: myurl,
					type: 'Get',
					datatype: "jsonp",
				}).trigger('reloadGrid');
				console.log(myurl);
				insertopertlog('拨测报告', 'Ftp下载测试报告', '查询', '', '');
			} else {
				$.messager.show({
					title: '提示！',
					msg: '请选择设备信息',
					showType: 'fade',
					style: {
						right: '',
						bottom: ''
					}
				})
			}
		} else {
			$.messager.show({
				title: '提醒',
				msg: '请选择正确的时间范围！',
				timeout: 1300,
				showType: 'slide',
				style: {
					right: '',
					bottom: ''
				}
			});
		}
	}

	var filter;
	jQuery("#QueryFtpDownloadId").jqGrid({
		url: IP + "GetProbeInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colNames: ['设备名称', '设备唯一编码'],
		colModel: [{
			name: 'deviceName',
			index: 'deviceName',
			sopt: ['eq'],
			width: 20
		}, {
			name: 'deviceid',
			index: 'deviceid',
			sopt: ['eq'],
			width: 40
		}],
		autowidth: false,
		shrinkToFit: true,
		//		shrinkToFit:false,
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 80,
		page: 1,
		viewrecords: true
	});
	var timer;
	$("#SearchCellFtpDownloadId").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryFtpDownloadId").jqGrid('filterInput', self.value);
		}, 0);
	});

	$("#QueryFtpDownloadArea").jqGrid({
		url: IP + "GetAreaInfoList?callback=?&session=" + session,
		datatype: "jsonp",
		height: 175,
		width: 383,
		colModel: [{
			label: '县市',
			name: 'text',
			sopt: ['eq'],
			width: 125
		}],
		//		rowNum: 1000,		
		loadonce: true,
		mtype: "GET",
		multiselect: true,
		multiselectWidth: 80,
		gridview: true,
		page: 1,
		viewrecords: true
			//		sortorder: "asc",
			//		sortname: 'id'
	});
	$("#SearchCellFtpDownloadArea").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryFtpDownloadArea").jqGrid('filterInput', self.value);
		}, 0);
	});


	$("#QueryFtpDownloadDefine").jqGrid({
		url: IP + "GetProbeGroupInfo?callback=?&session=" + session,
		mtype: "Get",
		datatype: "jsonp",
		colModel: [{
			label: '探针组',
			name: 'probeGroup',
			sopt: ['eq'],
			width: 50
		}, {
			label: '创建人',
			name: 'cPreson',
			sopt: ['eq'],
			width: 50
		}],
		loadonce: true,
		multiselect: true,
		multiselectWidth: 80,
		viewrecords: true,
		height: 175,
		width: 383,
		//			rowNum: 8,
		page: 1,
		loadtext: '正在加载数据',
	});
	$("#SearchCellFtpDownloadDefine").on("keyup", function() {

		var self = this;
		if (timer) {
			clearTimeout(timer);
		}
		timer = setTimeout(function() {
			//timer = null;
			$("#QueryFtpDownloadDefine").jqGrid('filterInput', self.value);
		}, 0);
	});
});
////--------------------------------------------tab按ID查询-----------------------------------------------------	
function showChildGrid(parentRowID, parentRowKey) { //两个子表都是传taskid
	var childGridID1 = parentRowID + "_table1";
	var childGridID2 = parentRowID + "_table2";
	var childGridPagerID1 = parentRowID + "_pager" + "1";
	var childGridPagerID2 = parentRowID + "_pager" + "2";
	childGridKey = $("#QueryFtpDownload").getCell(parentRowKey, 'taskid');
	// send the parent row primary key to the server so that we know which grid to show
	//		var childGridURL = IP + "QueryFtpDownloadDetail?callback=?&session=" + session + "&taskid=" + childGridKey;
	var childGridURL1 = IP + "QueryFtpDownProDetail?callback=?&taskid=" + childGridKey;
	var childGridURL2 = IP + "QueryFtpDownTrcDetail?callback=?&taskid=" + childGridKey;

	//	console.log(childGridURL);
	//childGridURL = childGridURL + "&parentRowID=" + encodeURIComponent(parentRowKey)
	// add a table and pager HTML elements to the parent grid row - we will render the child grid here
	$('#' + parentRowID).append('<div style=' + "width:500px" + '><table id=' + childGridID1 + '></table><div id=' + childGridPagerID1 + ' class=scroll></div></div>');
	$('#' + parentRowID).append('<div style=' + "width:500px" + '><table id=' + childGridID2 + '></table><div id=' + childGridPagerID2 + ' class=scroll></div></div>');
	$("#" + childGridID1).jqGrid({
		url: childGridURL1,
		mtype: "Get",
		datatype: "jsonp",
		page: 1,
		colModel: [{
			label: '过程',
			name: 'Progress'
		}, {
			label: '平均下载速度',
			name: 'averageDloadSpeeds'
		}, {
			label: '发送字节',
			name: 'sendBytes'
		}, {
			label: '接收字节',
			name: 'receivedBytes'
		}, {
			label: '百分比',
			name: 'totalPercent'
		}, {
			label: '当前速度',
			name: 'currentSpeed'
		}],
		loadonce: true,
		autowidth: true,
		rowNum: 10,
		height: '100%',
		pager: "#" + childGridPagerID1
	});

	$("#" + childGridID2).jqGrid({
		url: childGridURL2,
		mtype: "Get",
		datatype: "jsonp",
		page: 1,
		colModel: [{
			label: 'ts',
			name: 'ts'
		}, {
			label: '事件',
			name: 'event'
		}],
		loadonce: true,
		rowNum: 10,
		autowidth: true,
		height: '100%',
		pager: "#" + childGridPagerID2
	});
}